package cn.hahah.module.blog.controller.admin.usercollect;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.usercollect.vo.*;
import cn.hahah.module.blog.dal.dataobject.usercollect.UserCollectDO;
import cn.hahah.module.blog.service.usercollect.UserCollectService;

@Tag(name = "管理后台 - 用户收藏")
@RestController
@RequestMapping("/blog/user-collect")
@Validated
public class UserCollectController {

    @Resource
    private UserCollectService userCollectService;
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建用户收藏")
    @PreAuthorize("@ss.hasPermission('blog:user-collect:create')")
    public CommonResult<Long> createUserCollect(@Valid @RequestBody UserCollectSaveReqVO createReqVO) {
        return success(userCollectService.createUserCollect(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户收藏")
    @PreAuthorize("@ss.hasPermission('blog:user-collect:update')")
    public CommonResult<Boolean> updateUserCollect(@Valid @RequestBody UserCollectSaveReqVO updateReqVO) {
        userCollectService.updateUserCollect(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户收藏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-collect:delete')")
    public CommonResult<Boolean> deleteUserCollect(@RequestParam("id") Long id) {
        userCollectService.deleteUserCollect(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-collect:query')")
    public CommonResult<UserCollectRespVO> getUserCollect(@RequestParam("id") Long id) {
        UserCollectDO userCollect = userCollectService.getUserCollect(id);
        return success(BeanUtils.toBean(userCollect, UserCollectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户收藏分页")
    @PreAuthorize("@ss.hasPermission('blog:user-collect:query')")
    public CommonResult<PageResult<UserCollectRespVO>> getUserCollectPage(@Valid UserCollectPageReqVO pageReqVO) {
        PageResult<UserCollectDO> pageResult = userCollectService.getUserCollectPage(pageReqVO);
        PageResult<UserCollectRespVO> result = BeanUtils.toBean(pageResult, UserCollectRespVO.class);
        for (UserCollectRespVO userCollectRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userCollectRespVO.getUserId());
            ArticleDO articleDO = articleService.getArticle(userCollectRespVO.getArticleId());

            if( usersDO != null){
                userCollectRespVO.setUserName(usersDO.getUsername());
            }
            if( articleDO != null){
                userCollectRespVO.setArticleName(articleDO.getTitle());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户收藏 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-collect:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCollectExcel(@Valid UserCollectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCollectDO> list = userCollectService.getUserCollectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户收藏.xls", "数据", UserCollectRespVO.class,
                        BeanUtils.toBean(list, UserCollectRespVO.class));
    }

}